package com.qst.service.impl;

import com.qst.ExamException;
import com.qst.dao.AssessmentTypeDao;
import com.qst.dao.DAOFactory;
import com.qst.dao.QuestionDao;
import com.qst.entity.AssessmentType;
import com.qst.service.AssessmentService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssessmentServiceImpl implements AssessmentService {

    private AssessmentTypeDao assessmentDAO = DAOFactory.getDao(AssessmentTypeDao.class);
    private QuestionDao questionDAO = DAOFactory.getDao(QuestionDao.class);

    @Override
    public List<AssessmentType> findAllAssessment() {
        return assessmentDAO.findAll();
    }

    @Override
    public AssessmentType findAssessmentById(int id) {
        return assessmentDAO.findById(id);
    }

    @Override
    public void deleteAssessment(int id) {
        if (questionDAO.findCountByAssessment(id) > 0) {
            throw new ExamException("本考核类型已有试题，不能删除");
        }
        assessmentDAO.delete(id);
    }

    @Override
    public void saveAssessment(AssessmentType assessment) {
        AssessmentType temp = assessmentDAO.findByTitle(assessment.getTitle());
        if (temp != null) {
            throw new ExamException("考核类型已存在");
        }
        assessmentDAO.insert(assessment);
    }

    @Override
    public void updateAssessment(AssessmentType assessment) {
        AssessmentType temp = assessmentDAO.findByTitle(assessment.getTitle());
        if (temp != null && temp.getId() != assessment.getId()) {
            throw new ExamException("考核类型重复");
        }
        assessmentDAO.update(assessment);
    }

}
